<template>
	<view class="cart-box">
		<u-swipe-action class="cart-detail" v-for="(collect, index) in collectList" :key="collect.id"
			:btn-width="btnWidth" :options="options" :index="index" :show="collect.show" @click="click" @open="open"
		>
			<view class="goods">
				<view class="goods-img">
					<u-image width="300rpx" height="300rpx" :src="collect.goods.cover_url"></u-image>
				</view>
				<view class="cart-msg">
					<view class="goods-title u-line-1">{{collect.goods.title}}</view>
					<view class="goods-price">￥{{collect.goods.price}}</view>
				</view>
			</view>
			<u-line border-style="dotted" color="info"></u-line>
		</u-swipe-action>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: [{
					text: '取消',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				btnWidth:180,
				show: false,
				collectList: []
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			async getData(){
				const resp = await this.$u.api.collectList();
				this.collectList = resp.data
				this.collectList.forEach(item => this.$set(item,'show', false))
			},
			async click(index, index1) {
				// 调用API取消收藏商品
				await this.$u.api.goodsCollect(this.collectList[index].goods_id)
				this.$u.toast("取消成功")
				this.getData()
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				// this.collectList[index].show = true;
				this.$set(this.collectList[index],'show',true);
				this.collectList.map((val, idx) => {
					if(index != idx) this.$set(this.collectList[idx],'show',false);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.cart-box {
	padding: 20rpx 10rpx 10rpx;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	.cart-detail{
		display: flex;
		flex-direction: column;
		.goods{
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			justify-content: space-between;
			.goods-img{
				flex: 1;
				height: 300rpx;
			}
			.cart-msg{
				flex-grow: 2;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				height: 200rpx;
			}
			.cart-num{
				flex: 1;
			}
		}
	}
	padding-bottom: 100rpx;
}

</style>
